(function ($) {
  "use strict";

  jQuery.validator.addMethod(
    "answercheck",
    function (value, element) {
      return this.optional(element) || /^\bcat\b$/.test(value);
    },
    "type the correct answer -_-"
  );

  // validate contactForm form
  $(function () {
    $("#contactForm").validate({
      rules: {
        name: {
          required: true,
          minlength: 2,
        },
        name2: {
          required: true,
          minlength: 2,
        },
        email: {
          required: true,
          email: true,
        },
        number: {
          required: true,
          minlength: 12,
        },
        subject: {
          required: true,
          minlength: 12,
        },
        message: {
          required: true,
          minlength: 20,
        },
      },
      messages: {
        name: {
          required: "come on, your name, don't you?",
          minlength: "your name must consist of at least 2 characters",
        },
        subject: {
          required: "come on, you have a subject, don't you?",
          minlength: "please input",
        },
        email: {
          required: "no email, no message",
        },
        number: {
          required: "please input your phone number",
        },
        message: {
          required: "um...yea, you have to write something to send this form.",
          minlength: "thats all? really?",
        },
      },
      submitHandler: function (form) {
        $(form).ajaxSubmit({
          type: "POST",
          data: $(form).serialize(),
          url: "contact_process.php",
          success: function () {
            $("#contactForm :input").attr("disabled", "disabled");
            $("#contactForm").fadeTo("slow", 1, function () {
              $(this).find(":input").attr("disabled", "disabled");
              $(this).find("label").css("cursor", "default");
              $("#success").fadeIn();
            });
          },
          error: function () {
            $("#contactForm").fadeTo("slow", 1, function () {
              $("#error").fadeIn();
            });
          },
        });
      },
    });
  });
})(jQuery);
